import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { CookieService } from 'ngx-cookie-service';
import { Router } from '@angular/router';
import { UserService } from 'src/app/service/userService';

@Component({
  selector: 'app-blog',
  templateUrl: './blog.component.html',
  styleUrls: ['./blog.component.css'],
  encapsulation:ViewEncapsulation.None
})
export class BlogComponent implements OnInit {

  index: number;
  activeUsers: any[];
  isCollapse:boolean;
  constructor(private cookieService: CookieService,
              private router: Router,
              private userService: UserService) { }

  ngOnInit(): void {
    this.index = 0;
    this.initActiveUser();
  }

  initActiveUser(){
    this.userService.getActiveUsers(5).subscribe(datas=>{
      console.log(datas);
      this.activeUsers  = datas;
      for(let i = 0; i < this.activeUsers.length; i++){
        this.activeUsers[i].firstLetter = this.activeUsers[i].nickname.slice(0,1).toUpperCase();
      }
      this.isCollapse = false;
      console.log(this.activeUsers);
    });
  }

  gotoBlog(){
    if(this.cookieService.get("username") == null || this.cookieService.get("username") == ""){
      alert("请登录后查看！");
      return;
    }
    this.router.navigate(['/tutorial-editable-template'],{ queryParams: { name: this.queryBlogName() } });
  }
  queryBlogName(){
    return this.cookieService.get("username") + "-blog";
  }
  queryUserBlogName(username){
    return username + "-blog";
  }

  gotoUserBlog(username:string){
    this.router.navigate(['/tutorial-editable-template'],{ queryParams: { name: this.queryUserBlogName(username) } });
  }

  showAllUser(){
    this.userService.getActiveUsers(-1).subscribe(datas=>{
      this.activeUsers  = datas;
      for(let i = 0; i < this.activeUsers.length; i++){
        this.activeUsers[i].firstLetter = this.activeUsers[i].username.slice(0,1).toUpperCase();
      }
      this.isCollapse = true;
      console.log(this.activeUsers);
    });
  }

  foldUp(){
    this.initActiveUser();
  }
}
